package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IRegionService regionService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    /**
     * 查询热门区域
     * @param model
     * @return
     */
    @RequestMapping("")
    public String index(Model model) {

        List<Region> hotRegions = regionService.queryHotRegions();

        model.addAttribute("hotRegions", hotRegions);

        return "destination/index";
    }

    /**
     * 热门地区显示
     * @param model
     * @param regionId
     * @return
     * 我们的Ajax异步请求不止可以返回json类型的数据,也可以返回一个页面,局部刷新
     */
    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId) {
        List<Destination> destinations = destinationService.getDestByRegionId(regionId);
        //leftDests
        List<Destination> leftDests = new ArrayList<>();
        //rightDests
        List<Destination> rightDests = new ArrayList<>();
        //定义一个计数器
        int i = 0;
        //将destinations平分给leftDests和rightDests
        for (Destination destination : destinations) {
            if (i % 2 == 0) {
                leftDests.add(destination);
            } else {
                rightDests.add(destination);
            }
            i++;
        }

//        leftDests = destinations.subList(0, destinations.size() / 2 + 1);
//        rightDests = destinations.subList(destinations.size() / 2 + 1 , destinations.size());
        //将数据存入session
        model.addAttribute("leftDests",leftDests);
        model.addAttribute("rightDests",rightDests);
        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }

    /**
     * 用于查询面包屑
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/guide")
    public String guide(Model model, Long id) {
        //面包屑查询
        List<Destination> toasts = destinationService.getToasts(id);

        Destination dest = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);

        //catalogs
        model.addAttribute("catalogs",strategyCatalogService.getCatalogByDestId(id));

        //strategyDetails
        List<StrategyDetail> strategyDetails = strategyDetailService.getTTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);

        return "destination/guide";
    }


    //目的地上的面包屑导航
    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo) {
        //toasts
        //dest
        //面包屑查询
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);

        return "destination/survey";
    }


    /**
     * 概况
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo) {
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        //catalog
        for (StrategyCatalog catalog : catalogs) {
            if (catalog.getId() == qo.getCatalogId()) {
                model.addAttribute("catalog", catalog);
                //detail
                List<StrategyDetail> details = catalog.getDetails();
                StrategyDetail detail = details.get(0);
                detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                model.addAttribute("detail",detail);
            }
        }
        return "destination/surveyTpl";
    }

    /**
     * 异步分页,用户查看游记列表
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/travels")
    public String travels(Model model,TravelQuery qo) {

        PageInfo<Travel> pageInfo = travelService.query(qo);

        model.addAttribute("pageInfo",pageInfo);

        return "destination/travelTpl";
    }

}
